package com.huilunyingshi.videoconsumer.controller;


import com.huilunyingshi.videoapi.service.TUserService;
import com.hullunyingshi.until.ControllerResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;

@Slf4j
@Scope("prototype")
@RestController
@RequestMapping("/User")
public class UesrController {
    private final Logger logger = LoggerFactory.getLogger(AnimeController.class);

    @Autowired
    TUserService tUserService;

    //注册用户
    @ApiOperation(value = "注册用户", notes = "注册用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "user_avatar", value = "用户头像", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "user_email", value = "用户邮箱", paramType = "query", dataType = "String", required = false)

    })
    @PostMapping("insertSelective")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "创建成功", response = ControllerResult.class),
            @ApiResponse(code = 400, message = "创建失败", response = String.class),
            @ApiResponse(code = 500, message = "(服务器内部错误)服务器遇到错误，无法完成请求 ", response = String.class),
    })
    public ControllerResult insertSelective(String username, String password, String user_avatar, String user_email) throws ParseException {
        logger.info("#################### AnimeController. /Anime/getAnimeList" + username + "#######");

        try {

            return tUserService.insertSelective(username, password, user_avatar, user_email);
        } catch (Exception e) {
            logger.info("#################### error 500 /User/insertSelective" + e.getStackTrace());
            return new ControllerResult("500", "服务器错误", null);
        }
    }
    //登录用户
    @ApiOperation(value = "登录用户", notes = "登录用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "query", dataType = "String", required = false),

    })
    @PostMapping("recordUser")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "创建成功", response = ControllerResult.class),
            @ApiResponse(code = 400, message = "创建失败", response = String.class),
            @ApiResponse(code = 500, message = "(服务器内部错误)服务器遇到错误，无法完成请求 ", response = String.class),
    })
    public ControllerResult recordUser(String username, String password) throws ParseException {
        logger.info("#################### UesrController. /Uesr/recordUser" + username + "#######");

        try {

            return tUserService.recordUser(username, password);
        } catch (Exception e) {
            logger.info("#################### error 500 /User/insertSelective" + e.getStackTrace());
            return new ControllerResult("500", "服务器错误", null);
        }
    }

    @ApiOperation(value = "登出用户", notes = "登出用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", paramType = "query", dataType = "String", required = false),

    })
    @GetMapping("outUser")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "创建成功", response = ControllerResult.class),
            @ApiResponse(code = 400, message = "创建失败", response = String.class),
            @ApiResponse(code = 500, message = "(服务器内部错误)服务器遇到错误，无法完成请求 ", response = String.class),
    })
    public ControllerResult outUser(String username) throws ParseException {
        logger.info("#################### UesrController. /Uesr/outUser" + username + "#######");

        try {

            return tUserService.outUser(username);
        } catch (Exception e) {
            logger.info("#################### error 500 /User/insertSelective" + e.getStackTrace());
            return new ControllerResult("500", "服务器错误", null);
        }
    }

    @ApiOperation(value = "更改密码", notes = "更改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "oldpassword", value = "旧密码", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "newpassword", value = "新密码", paramType = "query", dataType = "String", required = false),

    })
    @PostMapping("updateByPassword")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "创建成功", response = ControllerResult.class),
            @ApiResponse(code = 400, message = "创建失败", response = String.class),
            @ApiResponse(code = 500, message = "(服务器内部错误)服务器遇到错误，无法完成请求 ", response = String.class),
    })
    public ControllerResult updateByPassword(String username, String oldpassword, String newpassword) throws ParseException {
        logger.info("#################### UesrController. /Uesr/updateByPassword" + username + "#######");

        try {

            return tUserService.updateBypassword(username,oldpassword,newpassword);
        } catch (Exception e) {
            logger.info("#################### error 500 /User/insertSelective" + e.getStackTrace());
            return new ControllerResult("500", "服务器错误", null);
        }
    }




}
